//
//  libSuperSDK.m
//  libSuperSDK
//
//  Created by wyht－ios－dev on 15/10/13.
//  Copyright © 2015年 WYHT. All rights reserved.
//

#import "libSuperSDK.h"
#import "SuperSDKInst.h"
#import "SSDKEventKeys.h"
#import "SSDKDataKeys.h"
#import "SSDKEventManager.h"
#import "SuperSDKPluginTools/SSDKPluginLogUtil.h"

@implementation SuperSDK

+ (void) initWithAppId:(NSString *)appId
             appSecret:(NSString *)appSecret
            privateKey:(NSString *)privateKey
          userDelegate:(id<SSDKDelegate>)userDelegate
           payDelegate:(id<SSDKDelegate>)payDelegate
             debugMode:(BOOL) debugMode {
    [[SuperSDKInst getInstance] initWithAppId:appId appSecret:appSecret privateKey:privateKey userDelegate:userDelegate payDelegate:payDelegate debugMode:debugMode];
}

+ (BOOL) isSupported:(NSString *) message {
    [SSDKPluginLogUtil Log:[@"SSDK_isSupport :" stringByAppendingString:message]];
    return [SSDKEventManager isSupportEvent:[[SSDKEventManager getInstance] targetSdkName] eventName:message];
}

//发布版本号
+ (NSString *) getVersionName {
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}

//内部版本号
+ (NSString *) getVersionCode {
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
}

//用户自定义Plist中的值
+ (NSString *) getUserDefinedValue:(NSString *)key {
    NSDictionary *data = [[NSBundle mainBundle] infoDictionary];
    return [data valueForKey:key];
}

+ (void) call:(NSString *) message
         with:(id) obj {
    dispatch_async(dispatch_get_main_queue(), ^{
        [SSDKPluginLogUtil Log:[@"SSDK_call :" stringByAppendingString:message]];
        [SSDKEventManager postNotificationName:message object:obj];
    });
}

+ (void) login:(NSString *)loginReqUrl appData:(NSString *)data {
    [[SuperSDKInst getInstance] login:loginReqUrl data:data];
}

+ (void) login:(int)serverID loginReqUrl:(NSString *)loginReqUrl appData:(NSString *)data {
    [[SuperSDKInst getInstance] login:serverID loginReqUrl:loginReqUrl data:data];
}

+ (void) login:(NSString *) data {
    [[SuperSDKInst getInstance] login:data];
}

+ (BOOL) isLogin {
    return [[SuperSDKInst getInstance] isLogin];
}

+ (NSString *) uid {
    return [[SuperSDKInst getInstance] uid];
}

+ (NSString *) token {
    return [[SuperSDKInst getInstance] token];
}

+ (void) destory {
    [[SuperSDKInst getInstance] destroy];
}

+ (void) payWithPayInfo:(SSDKPayInfo *) payInfo {
    [[SuperSDKInst getInstance] payWithPayInfo:payInfo];
}

+ (BOOL) application:(id)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    //同时Post出事件，下面的返回值只处理SDK class的情况
    NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
    [dic setValue:application forKey:SSDK_E_APPLICATION];
    [dic setValue:url forKey:SSDK_E_OPENURL];
    [dic setValue:sourceApplication forKey:SSDK_E_SOURCEAPPLICATION];
    [dic setValue:annotation forKey:SSDK_E_ANNOTATION];
    [SSDKEventManager postNotificationName:EVENT_APPLICATION_OPENURL_SOURCEAPPLICATION_ANNOTATION object:dic];
    return [SSDKEventManager application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
}

+ (BOOL) application:(id)application handleOpenURL:(NSURL *)url {
    //同时Post出事件，下面的返回值只处理SDK class的情况
    NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
    [dic setValue:application forKey:SSDK_E_APPLICATION];
    [dic setValue:url forKey:SSDK_E_HANDLEOPENURL];
    [SSDKEventManager postNotificationName:EVENT_APPLICATION_HANDLEOPENURL object:dic];
    return [SSDKEventManager application:application handleOpenURL:url];
}

+ (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //同时Post出事件，下面的返回值只处理SDK class的情况
    NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
    [dic setValue:application forKey:SSDK_E_APPLICATION];
    [dic setValue:launchOptions forKey:SSDK_E_DIDFINISHLAUNCHWITHOPTIONS];
    [SSDKEventManager postNotificationName:EVENT_APPLICATION_DIDFINISHLAUNCHINGWITHOPTIONS object:dic];
    return [SSDKEventManager application:application didFinishLaunchingWithOptions:launchOptions];
}

+ (BOOL) application:(id)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
    //同时Post出事件，下面的返回值只处理SDK class的情况
    NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
    [dic setValue:app forKey:SSDK_E_APPLICATION];
    [dic setValue:url forKey:SSDK_E_OPENURL];
    [dic setValue:options forKey:SSDK_E_OPTIONS];
    [SSDKEventManager postNotificationName:EVENT_APPLICATION_OPENURL_OPTIONS object:dic];
    return [SSDKEventManager application:app openURL:url options:options];
}

+ (void) applicationWillEnterForeground:(id)application {
    NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
    [dic setValue:application forKey:SSDK_E_APPLICATION];
    [SSDKEventManager postNotificationName:EVENT_APPLICATIONWILLENTERFOREGROUND object:dic];
}

+ (void) applicationDidEnterBackground:(id)application {
    NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
    [dic setValue:application forKey:SSDK_E_APPLICATION];
    [SSDKEventManager postNotificationName:EVENT_APPLICATIONDIDENTERBACKGROUND object:dic];
}

+ (void) applicationDidBecomeActive:(UIApplication *)application {
    NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
    [dic setValue:application forKey:SSDK_E_APPLICATION];
    [SSDKEventManager postNotificationName:EVENT_APPLICATIONDIDBECOMEACTIVE object:dic];
}

+ (void) application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
    [dic setValue:application forKey:SSDK_E_APPLICATION];
    [dic setValue:notificationSettings forKey:SSDK_E_NOTIFICATIONSETTINGS];
    [SSDKEventManager postNotificationName:EVENT_APPLICATION_DIDREGISTERUSERNOTIFICATIONSETTINGS object:dic];
}

+ (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
    [dic setValue:application forKey:SSDK_E_APPLICATION];
    [dic setValue:deviceToken forKey:SSDK_E_DEVICETOKEN];
    [SSDKEventManager postNotificationName:EVENT_APPLICATION_DIDREGISTERFORREMOTENOTIFICATIONSWITHDEVICETOKEN object:dic];
}

+ (void) applicationWillTerminate:(id)application {
    NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
    [dic setValue:application forKey:SSDK_E_APPLICATION];
    [SSDKEventManager postNotificationName:EVENT_APPLICATION_WILL_TERMINATE object:dic];
}

+ (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
    [dic setValue:application forKey:SSDK_E_APPLICATION];
    [dic setValue:userInfo forKey:SSDK_E_USERINFO];
    [SSDKEventManager postNotificationName:EVENT_APPLICATION_DIDRECEIVEREMOTENOTIFICATION object:dic];
}

+ (void) application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
    [dic setValue:application forKey:SSDK_E_APPLICATION];
    [dic setValue:error forKey:SSDK_E_ERROR];
    [SSDKEventManager postNotificationName:EVENT_APPLICATION_DIDFAILTOREGISTERFORREMOTENOFIFICATIONSWITHERROR object:dic];
}

+(void)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity {
    NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
    [dic setValue:application forKey:SSDK_E_APPLICATION];
    [dic setValue:userActivity forKey:SSDK_E_USERACTIVITY];
    [SSDKEventManager postNotificationName:EVENT_APPLICATION_CONTINUEUSERACTIVITY object:dic];
}

+ (void) application:(id)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
    NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
    [dic setValue:application forKey:SSDK_E_APPLICATION];
    [dic setValue:identifier forKey:SSDK_E_USERINFO];
    [dic setValue:userInfo forKey:SSDK_E_IDENTIFIER];
    [dic setValue:completionHandler forKey:SSDK_E_HANDLER];
    [SSDKEventManager postNotificationName:EVENT_APPLICATION_HANDLEACTIONWITHIDENTIFIER_FORREMOTENOTIFICATION_COMPLETIONHANDLE object:dic];
}

+ (void) application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
    [dic setValue:application forKey:SSDK_E_APPLICATION];
    [dic setValue:userInfo forKey:SSDK_E_USERINFO];
    [dic setValue:completionHandler forKey:SSDK_E_HANDLER];
    [SSDKEventManager postNotificationName:EVENT_APPLICATION_DIDRECEIVEREMOTENOTIFICATION_FETCHCONPLETIONHANDLER object:dic];
}

@end
